package com.lipeng.api;

import com.lipeng.bean.*;
import com.lipeng.service.*;
import com.lipeng.utils.AddPic;
import com.lipeng.utils.AjaxResult;
import com.lipeng.utils.IPUtils;
import com.lipeng.utils.StrUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * Created by dllo on 18/3/16.
 */
@ResponseBody
@RequestMapping(value = "/api/")
@Controller
public class UserControllerAPI {

    @Resource
    private UserService userService;
    @Resource
    private OrderService orderService;
    @Resource
    private GameService gameService;
    @Resource
    private PhotoService photoService;
    @Resource
    private ClubService clubService;
    @Resource
    private ShopService shopService;

    //用户登录验证
    @RequestMapping(value = "login")
    public AjaxResult doLogin(@RequestParam("uname") String uname, String upsw) {
        return userService.doLogin(uname, upsw);
    }

    //用户注册
    @RequestMapping(value = "adduser")
    public AjaxResult adduser(@RequestParam("uname") String uname,
                              @RequestParam("upsw") String upsw,
                              @RequestParam("upsw1") String upsw1) {
        AjaxResult result = new AjaxResult();
        if (!upsw.equals(upsw1)) {
            result.setCode("0");
            result.setMessage("密码不一致");
            return result;
        }
        if (userService.findUserByName(uname) != null) {
            result.setCode("0");
            result.setMessage("用户名已存在");
            return result;
        }
        if (StrUtils.isNull(uname) || StrUtils.isNull(upsw)) {
            result.setCode("0");
            result.setMessage("请填入用户名和密码");
            return result;
        }
        User user = new User();
        user.setUname(uname);
        user.setUpsw(upsw);
        user.setUstate(1);
        int num = userService.addUser(user);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("注册失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("注册成功");
        return result;
    }


    //查看用户信息
    @RequestMapping(value = "selectuser")
    public AjaxResult selectUser(@RequestParam("uid") Integer uid) {
        return userService.selectUserById(uid);
    }


    //修改用户信息
    @RequestMapping(value = "updateuser")
    public AjaxResult updateUser(@RequestParam(value = "uid") Integer uid,
                                 @RequestParam(value = "usex", required = false) String usex,
                                 @RequestParam(value = "utel", required = false) String utel,
                                 @RequestParam(value = "uintro", required = false) String uintro,
                                 @RequestParam(value = "uadress", required = false) String uadress,
                                 @RequestParam("img") MultipartFile file, HttpServletRequest request) throws IOException {
        User user = new User();
        user.setUid(uid);
        user.setUsex(usex);
        user.setUtel(utel);
        user.setUintro(uintro);
        user.setUadress(uadress);
        if (file != null) {
            System.out.println("~~~~~~~~~~~~~~~~~");
            String sqlPath = AddPic.addPic(file, user);
            user.setUpic(sqlPath);
        }
        return userService.updateUser(user);
    }


    //查看我的订单
    @RequestMapping(value = "myorder")
    public AjaxResult MyOrders(@RequestParam("uid") Integer uid) {
        AjaxResult result = new AjaxResult();
        List<Order> orderList = orderService.selectOrderByUid(uid);
        if (orderList.isEmpty()) {
            result.setCode("1");
            result.setMessage("暂无订单,快去下单吧");
            return result;
        }
        for (Order order : orderList) {
            String pic = order.getShop().getSpic();
            if (!pic.substring(0,9).equals(IPUtils.getIP().substring(0,9))){
                String spic = IPUtils.getIP() + ":8080" + order.getShop().getSpic();
                order.getShop().setSpic(spic);
            }
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(orderList);
        return result;
    }

    //查看详细订单
    @RequestMapping(value = "orderdetail")
    public AjaxResult orderDetail(@RequestParam("oid") Integer oid) {
        AjaxResult result = new AjaxResult();
        Order order = orderService.selectOrderById(oid);
        if (order == null) {
            result.setCode("0");
            result.setMessage("查看失败");
            return result;
        }
        order.getShop().setSpic(IPUtils.getIP() + ":8080" + order.getShop().getSpic());
        order.getUser().setUpic(IPUtils.getIP() + ":8080" + order.getUser().getUpic());
        result.setCode("1");
        result.setMessage("查看成功");
        result.setData(order);
        return result;
    }

    //查看我的购票
    @RequestMapping(value = "mytickets")
    public AjaxResult MyTickets(@RequestParam("uid") Integer uid) {
        AjaxResult result = new AjaxResult();
        List<Tickets> ticketList = gameService.selectTicketsByUid(uid);
        if (ticketList.isEmpty()) {
            result.setCode("1");
            result.setMessage("暂无门票,快去购票吧");
            return result;
        }
        for (Tickets tickets : ticketList) {
            String pic = tickets.getGame().getGpic();
            if (!pic.substring(0,9).equals(IPUtils.getIP().substring(0,9))) {
                String gpic = IPUtils.getIP() + ":8080" + tickets.getGame().getGpic();
                tickets.getGame().setGpic(gpic);
            }
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(ticketList);
        return result;
    }

    //查看购票详细信息
    @RequestMapping(value = "ticketsdetail")
    public AjaxResult ticketsDetail(@RequestParam("tid") Integer tid) {
        AjaxResult result = new AjaxResult();
        Tickets tickets = gameService.selectTicketsByTid(tid);
        if (tickets == null) {
            result.setCode("0");
            result.setMessage("查看失败");
            return result;
        }
        tickets.getGame().setGpic(IPUtils.getIP() + ":8080" + tickets.getGame().getGpic());
        result.setCode("1");
        result.setMessage("查看成功");
        result.setData(tickets);
        return result;
    }


    //我的收藏
    @RequestMapping(value = "allusercollect")
    public AjaxResult selectCollectByUid(@RequestParam("uid") Integer uid) {
        return shopService.selectCollectByUid(uid);
    }

    //我的关注
    @RequestMapping(value = "alluserattention")
    public AjaxResult selectAttentionByUid(@RequestParam("uid") Integer uid) {
        return clubService.selectAttentionByUid(uid);
    }


    //查看我的晒图
    @RequestMapping(value = "myphoto")
    public AjaxResult MyPhoto(@RequestParam("uid") Integer uid) {
        AjaxResult result = new AjaxResult();
        List<Photo> photoList = photoService.selectPhotoByUid(uid);
        if (photoList.isEmpty()) {
            result.setCode("1");
            result.setMessage("暂无晒图,快去发图吧");
            return result;
        }
        for (Photo photo : photoList) {
            photo.setPic(IPUtils.getIP() + ":8080" + photo.getPic());
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(photoList);
        return result;
    }

}
